$(document).ready(function() {
    var fname = document.getElementById("fname");
    var lname = $("#lname");
    var uname = $("#uname");
    var email = $("#email");
    var password = $("#password");
    var cpassword = $("#cpassword");
    var address = $("#address");
    var phone = $("#phone");
    var mobile = $("#mobile");
    var cbalance = $("#cbalance");
    var img = $("#pimg");
    var code = $("#pcode");

    fname.blur(validatefName());
    lname.blur(validatelName());
    uname.blur(validateuName());
    email.blur(validateEmail());
    password.blur(validatePass1());
    cpassword.blur(validatePass2());
    address.blur(validateaddress());

    mobile.blur(validatenumber());
    cbalance.blur(validatecbalance());
    phone.blur(validatephnumber());
    code.blur(validatecode());
    //On key press
    fname.keyup(validatefName());
    lname.keyup(validatelName());
    uname.keyup(validateuName());
    email.keyup(validateEmail());
    password.keyup(validatePass1());
    cpassword.keyup(validatePass2());
    address.keyup(validateaddress());

    mobile.keyup(validatenumber());
    phone.keyup(validatephnumber());
    cbalance.keyup(validatecbalance());
    code.keyup(validatecode());



    $("#signupbtn").click(function() {

        if (validatefName() & validatelName() & validateuName() & validateEmail() & validatePass1() & validatePass2()
                & validateaddress() & validatenumber() & validatecbalance() & validatephnumber() & validatecode()) {
            var data = {
                fname: $("#fname").val(),
                lname: $("#lname").val(),
                uname: $("#uname").val(),
                email: $("#email").val(),
                password: $("#password").val(),
                address: $("#address").val(),
                phone: $("#phone").val(),
                mobile: $("#mobile").val(),
                code: $("#pcode").val(),
                balance: $("#cbalance").val()
            };
        }

    });



    //validation functions
    function validateEmail() {
        //testing regular expression
        var a = $("#email").val();
        var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
        //if it's valid email
        if (filter.test(a)) {
            email.removeClass("error");
            return true;
        }
        //if it's NOT valid
        else {
            email.addClass("error");
            return false;
        }
    }
    function validatefName() {
        //if it's NOT valid
        if (document.getElementById("fname").value.length < 4) {
            //fname.addClass("error");
            document.getElementById("fnameer").style.display="hidden";
            return false;
        }
        //if it's valid
        else {
//            fname.removeClass("error");
            document.getElementById("fnameer").style.display="inline";
            return true;
        }
    }
    function validatelName() {
        //if it's NOT valid
        if (lname.val().length < 4) {
            lname.addClass("error");
            return false;
        }
        //if it's valid
        else {
            lname.removeClass("error");
            return true;
        }
    }
    function validateuName() {
        //if it's NOT valid
        if (uname.val().length < 4) {
            uname.addClass("error");
            return false;
        }
        //if it's valid
        else {
            uname.removeClass("error");
            return true;
        }
    }
    function validatePass1() {


        //it's NOT valid
        if (password.val().length < 7) {
            password.addClass("error");
            return false;
        }
        //it's valid
        else {
            password.removeClass("error");
            validatePass2();
            return true;
        }
    }
    function validatePass2() {

        //are NOT valid
        if (password.val() != cpassword.val()) {
            cpassword.addClass("error");
            return false;
        }
        //are valid
        else {
            cpassword.removeClass("error");
            return true;
        }
    }
    function validateaddress() {
        //it's NOT valid
        if (address.val().length < 4) {
            address.addClass("error");
            return false;
        }
        //it's valid
        else {
            address.removeClass("error");
            return true;
        }
    }
    function validatebirthdate() {
        //it's NOT valid
        if (birthdate.val().length < 4) {
            birthdate.addClass("error");
            return false;
        }
        //it's valid
        else {
            birthdate.removeClass("error");
            return true;
        }
    }
    function validatecbalance() {
        //it's NOT valid
        if (cbalance.val().length < 4) {
            cbalance.addClass("error");
            return false;
        }
        //it's valid
        else {
            cbalance.removeClass("error");
            return true;
        }
    }
    function validatenumber() {
        //it's NOT valid
        if (mobile.val().length != 11) {
            mobile.addClass("error");
            return false;
        }
        //it's valid
        else {
            mobile.removeClass("error");
            return true;
        }
    }

    function validatephnumber() {
        //it's NOT valid
        if (phone.val().length != 8) {
            phone.addClass("error");
            return false;
        }
        //it's valid
        else {
            phone.removeClass("error");
            return true;
        }
    }
    function validatecode() {
        //it's NOT valid
        if (code.val().length != 5) {
            code.addClass("error");
            return false;
        }
        //it's valid
        else {
            code.removeClass("error");
            return true;
        }
    }


});